package handler

import (
	"encoding/json"
	"home-ctrl-sys/cachae"
	"home-ctrl-sys/modler"
	"home-ctrl-sys/pkg"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	redis_user_ley := "redis_user" + username
	errCount, _ := cachae.Rdb.Get(cachae.Ctx, redis_user_ley).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"mas":  "密码错误3次",
			"code": 500,
		})
		return
	}

	var user modler.User
	err := modler.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "查询失败",
			"code": 500,
		})
		return
	}

	if user.ID == 0 {
		c.JSON(200, gin.H{
			"mas":  "账号或密码错误",
			"code": 500,
		})
		return
	}

	if user.Password != pkg.MD5(password) {
		cachae.Rdb.Incr(cachae.Ctx, redis_user_ley)
		if errCount == 2 {
			cachae.Rdb.Set(cachae.Ctx, redis_user_ley, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"mas":  "密码错误3次，封禁2小时",
				"code": 500,
			})
			return
		}
		c.JSON(200, gin.H{
			"mas":  "密码错误",
			"code": 500,
		})
		return

	}

	c.JSON(200, gin.H{
		"mas":  "密码正确",
		"code": 200,
		"data": user,
	})
	return

}

func Rode(c *gin.Context) {
	uid := c.Query("uid")
	/*
			SELECT
		  *
		FROM
		  rodes
		  LEFT JOIN user_rodes ON user_rodes.rid = rodes.id
		WHERE
		  user_rodes.uid =1
	*/
	var data modler.Rode
	modler.DB.Table("rodes").
		Joins("LEFT JOIN user_rodes ON user_rodes.rid = rodes.id").
		Where("user_rodes.uid = ?", uid).
		Debug().
		Find(&data)

	c.JSON(200, gin.H{
		"mas":  "角色信息查询成功",
		"code": 200,
		"data": data,
	})
	return

}

func Node(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN rode_nodes ON rode_nodes.nid = nodes.id
		WHERE
		  rode_nodes.rid =1
	*/
	var data modler.Node
	modler.DB.Table("nodes").
		Joins("LEFT JOIN rode_nodes ON rode_nodes.nid = nodes.id").
		Where("rode_nodes.rid = ?", rid).
		Debug().
		Find(&data)
	c.JSON(200, gin.H{
		"mas":  "权限信息查询成功",
		"code": 200,
		"data": data,
	})
	return

}

func CopeAdd(c *gin.Context) {
	CopeName := c.PostForm("CopeName")
	CopesName := c.PostForm("CopesName")

	cope := modler.Cope{
		CopeName:  CopeName,
		CopesName: CopesName,
	}

	modler.DB.Create(&cope)
	c.JSON(200, gin.H{
		"mas":  "添加成功",
		"code": 200,
		"data": cope,
	})

}

func OrderAdd(c *gin.Context) {
	OrderHao := c.PostForm("OrderHao")
	OrderName := c.PostForm("OrderName")
	OrdersName := c.PostForm("OrdersName")
	OrderTime := c.PostForm("OrderTime")
	Pid, _ := strconv.Atoi(c.PostForm("Pid"))

	order := modler.Order{
		OrderHao:   OrderHao,
		OrderName:  OrderName,
		OrdersName: OrdersName,
		OrderTime:  OrderTime,
		Pid:        Pid,
	}

	modler.DB.Create(&order)
	c.JSON(200, gin.H{
		"mas":  "添加成功",
		"code": 200,
		"data": order,
	})

}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var order []modler.Order
	redis_list_ley := "redis_list_err" + strconv.Itoa(page)
	result, _ := cachae.Rdb.Get(cachae.Ctx, redis_list_ley).Result()
	if result == "" {
		modler.DB.Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(order)
		cachae.Rdb.Set(cachae.Ctx, redis_list_ley, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &order)
	}

	var total int64
	modler.DB.Model(&modler.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"mas":   "成功",
		"code":  200,
		"data":  order,
		"total": total,
	})

}

func Login(c *gin.Context) {
	id := c.Query("id")
	var order modler.Order
	redis_list_ley := "redis_list_err" + id
	result, _ := cachae.Rdb.Get(cachae.Ctx, redis_list_ley).Result()
	if result == "" {
		modler.DB.Where("id=?", id).First(&order)
		jsonData, _ := json.Marshal(order)
		cachae.Rdb.Set(cachae.Ctx, redis_list_ley, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &order)
	}

	c.JSON(200, gin.H{
		"mas":  "搜索成功",
		"code": 200,
		"data": order,
	})
}
